package com.michael.retail.auth.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * 类功能描述:
 * <pre>
 *   SpringSecurity 配置
 * </pre>
 *
 * @author Michael
 * @version 1.0
 * @date 2021/7/6 14:58
 */
@Slf4j
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .requestMatchers(EndpointRequest.toAnyEndpoint()).permitAll()
            // 不需要认证的路径配置
            .antMatchers("/valid/**").permitAll()
            .antMatchers("/oauth/**").permitAll()
            .antMatchers("/rsa/publicKey").permitAll()
            .anyRequest().authenticated()
            .and().csrf().disable()
        ;
    }

    @Bean
    public BCryptPasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    public AuthenticationManager initAuthenticationManager() throws Exception {
        return super.authenticationManagerBean();
    }
}
